package com.util.checkdata;

/**
 * @Description: 错误：返回状态码和和信息
 *               成功：返回数据
 * @Author: yangzhiwei
 * @Date: 2021/4/26 14:13
 */
public class CommonCheckResult<T> {
    private CommonCheckResult() {

    }

    /* **** 成功：设置数据  失败：设置消息 *****/
    private T data;
    // 没有消息就是最好的消息
    private String message;

    private boolean checkedException;

    public T getSuccessData() {
        return data;
    }

    public boolean isCheckedException() {
        return checkedException;
    }

    public String getErrorMessage() {
        return this.message;
    }

    private void setSuccessData(T data) {
        this.checkedException = false;
        this.data = data;
    }

    private void setSuccess() {
        this.checkedException = false;
    }

    private void setErrorMessage(String... args) {
        this.checkedException = true;
        for (String s : args) {
            this.message = this.message + s;
        }
    }

//    public JsonResult checkResultToJsonResult() {
//        if (this.isCheckedException()) {
//            return JsonResult.renderError_S(this.getErrorMessage());
//        } else {
//            return JsonResult.renderSuccess_S("",this.getSuccessData());
//        }
//    }

    public static<T> CommonCheckResult success (T data) {
        CommonCheckResult checkResult = new CommonCheckResult();
        checkResult.setSuccessData(data);
        return checkResult;
    }

    public static CommonCheckResult failed (String message) {
        CommonCheckResult checkResult = new CommonCheckResult();
        checkResult.setErrorMessage(message);
        return checkResult;
    }

    public static<T> CommonCheckResult success () {
        CommonCheckResult checkResult = new CommonCheckResult();
        checkResult.setSuccess();
        return checkResult;
    }
}
